package com.webcash.CNBoard.Controller;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import com.webcash.CNBoard.Model.Login;
import com.webcash.CNBoard.Service.EmployeeService;
import com.webcash.CNBoard.Validation.LoginValidator;

@Controller
@RequestMapping("/login")
@SessionAttributes("login")
public class EmployeeLoginController {


	@Autowired
	LoginValidator loginValidator;

	@RequestMapping(method = RequestMethod.GET)
	public String action( Model model) {

		model.addAttribute("login", new Login());

		return "loginForm";
	}

	@RequestMapping(method = RequestMethod.POST)
	public String joinAction(@ModelAttribute @Valid Login login,
			BindingResult result, Model model) {

		loginValidator.validate(login, result);
		boolean admin = false;
		if(login.getId().equalsIgnoreCase("admin")&&login.getPassword().equalsIgnoreCase("admin")){
			admin=true;
		}

		if (result.hasErrors())
			return "loginForm";
		else if(admin){
			return "redirect:/admin";
		}
		else
			return "redirect:/employee";
	}
}
